package clases.academico;

import CapaDatos.DataSource;
import CapaDatos.Sql;
import clases.incio.ButtonEditor;
import clases.incio.ButtonRenderer;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JTable;
import javax.swing.SwingConstants;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;

public class Academico extends Alumno {

    private String tipoAcademico;
    private String descripcionAcademico;
    private DataSource dt;
    private String sql;

    public Academico() {
        dt = new DataSource();
    }

    public String getTipoAcademico() {
        return tipoAcademico;
    }

    public void setTipoAcademico(String tipo) {
        this.tipoAcademico = tipo;
    }

    public String getDescripcionAcademico() {
        return descripcionAcademico;
    }

    public void setDescripcionAcademico(String descripcion) {
        this.descripcionAcademico = descripcion;
    }

    private int MaxIdAcademico() {
        return Sql.getMax("IDACADEMICO", "ACADEMICO", true);
    }

    public boolean ExistenciaAcademico(String dato, String cedula) {
        sql = "SELECT * FROM ACADEMICO JOIN ALUMNO "
                + "ON ALUMNO.CEDULARUC=ACADEMICO.CEDULARUC "
                + "WHERE ACADEMICO.CEDULARUC='" + cedula + "' "
                + "AND TIPO='" + dato + "' ";
        return Sql.isExiste(sql);
    }

    public boolean guardarAcademico() {
        sql = "INSERT INTO ACADEMICO VALUES('" + this.MaxIdAcademico()
                + "','" + this.getCedulaAlumno() + "','" + tipoAcademico + "', "
                + "'" + descripcionAcademico + "')";
        return Sql.insertar(sql);
    }

    public boolean eliminarAcademico() {
        sql = "DELETE FROM ACADEMICO WHERE CEDULARUC = '" + this.getCedulaAlumno() + "'";
        return Sql.eliminar(sql);
    }

    public boolean eliminarAcademico(String dato, String cedula) {
        if (global.Mensajes.mostrarPregunta("¿Realmente desea eliminar el dato Académico del Alumno?") == true) {
            sql = "DELETE FROM ACADEMICO WHERE CEDULARUC = '" + cedula + "' AND TIPO='" + dato + "'";
            return Sql.eliminar(sql);
        }
        return false;
    }

    public boolean actualizarAcademico(String cedula) {
        sql = "UPDATE ACADEMICO SET TIPO='" + this.tipoAcademico + "', "
                + "DESCRIPCION='" + this.descripcionAcademico + "', "
                + "WHERE CEDULARUC='" + cedula + "'";
        return Sql.actualizar(sql);
    }
    
    public void listarDatoAcademico(JComboBox cbo) {
        cbo.removeAllItems();
        cbo.addItem("--Seleccione--");
        sql = "SELECT DISTINCT TIPO FROM ACADEMICO";

        Sql.getConsulta(sql, dt);
        while (dt.next()) {
            cbo.addItem(dt.getString(1));
        }
        cbo.setSelectedIndex(0);
    }

    public void llenardatosAcademicos(DefaultTableModel modelo, JTable tabla, String cedula) {
        sql = "SELECT TIPO, DESCRIPCION FROM ACADEMICO JOIN ALUMNO "
                + "ON ALUMNO.CEDULARUC=ACADEMICO.CEDULARUC "
                + "WHERE ALUMNO.CEDULARUC='" + cedula + "'";

        Sql.getConsulta(sql, dt);
        String[] fila;

        while (dt.next()) {
            fila = new String[2];
            fila[0] = dt.getString(1);
            fila[1] = dt.getString(2);

            tabla.getColumnModel().getColumn(2).setCellRenderer(new ButtonRenderer());
            tabla.getColumnModel().getColumn(2).setCellEditor(
                    new ButtonEditor(new JCheckBox(), modelo, tabla));

            modelo.addRow(fila);

        }
    }

    public String SqlGeneralAcademico() {
        sql = "SELECT ALUMNO.CEDULARUC, CONCAT(APELLIDO,' ',NOMBRE) AS ALUMNO, TIPO, DESCRIPCION \n"
                + "FROM ACADEMICO JOIN ALUMNO ON ALUMNO.CEDULARUC=ACADEMICO.CEDULARUC \n"
                + "JOIN PERSONA ON PERSONA.CEDULARUC=ALUMNO.CEDULARUC ORDER BY(ALUMNO)";
        return sql;
    }

    public String SqlCedulaAcademico(String cedula) {
        sql = "SELECT ALUMNO.CEDULARUC, CONCAT(APELLIDO,' ',NOMBRE) AS ALUMNO, TIPO, DESCRIPCION \n"
                + "FROM ACADEMICO JOIN ALUMNO ON ALUMNO.CEDULARUC=ACADEMICO.CEDULARUC \n"
                + "JOIN PERSONA ON PERSONA.CEDULARUC=ALUMNO.CEDULARUC "
                + "WHERE ALUMNO.CEDULARUC LIKE ('" + cedula + "%') ORDER BY(ALUMNO)";
        return sql;
    }

    public String SqlAlumnoAcademico(String alumno) {
        sql = "SELECT ALUMNO.CEDULARUC, CONCAT(APELLIDO,' ',NOMBRE) AS ALUMNO, TIPO, DESCRIPCION \n"
                + "FROM ACADEMICO JOIN ALUMNO ON ALUMNO.CEDULARUC=ACADEMICO.CEDULARUC \n"
                + "JOIN PERSONA ON PERSONA.CEDULARUC=ALUMNO.CEDULARUC  "
                + "WHERE CONCAT(APELLIDO,' ',NOMBRE) LIKE ('" + alumno + "%') ORDER BY(ALUMNO)";
        return sql;
    }

    public String SqlDatoAcademico(String dato) {
        sql = "SELECT ALUMNO.CEDULARUC, CONCAT(APELLIDO,' ',NOMBRE) AS ALUMNO, TIPO, DESCRIPCION \n"
                + "FROM ACADEMICO JOIN ALUMNO ON ALUMNO.CEDULARUC=ACADEMICO.CEDULARUC \n"
                + "JOIN PERSONA ON PERSONA.CEDULARUC=ALUMNO.CEDULARUC "
                + "WHERE TIPO = '" + dato + "' ORDER BY(ALUMNO)";
        return sql;
    }

    public void ConsultaAcademico(DefaultTableModel modelo, JTable tabla, String sql, JLabel Lpaginatotal, JLabel Lpagina, int pagina) {
        modelo = new DefaultTableModel() {
            @Override
            public boolean isCellEditable(int fila, int columna) {
                return false;
            }
        };
        tabla.setModel(modelo);
        modelo.addColumn("Nº");
        modelo.addColumn("CEDULA");
        modelo.addColumn("ALUMNO");
        modelo.addColumn("DATO ACADEMICO");
        modelo.addColumn("DESCRIPCION");

        DefaultTableCellRenderer centrar = new DefaultTableCellRenderer();
        centrar.setHorizontalAlignment(SwingConstants.CENTER);
        tabla.getColumnModel().getColumn(0).setCellRenderer(centrar);
        TableColumn columna = new TableColumn();
        columna = tabla.getColumnModel().getColumn(0);
        columna.setPreferredWidth(20);
        columna = tabla.getColumnModel().getColumn(1);
        columna.setPreferredWidth(40);
        columna = tabla.getColumnModel().getColumn(2);
        columna.setPreferredWidth(250);
        columna = tabla.getColumnModel().getColumn(3);
        columna.setPreferredWidth(250);
        columna = tabla.getColumnModel().getColumn(4);
        columna.setPreferredWidth(400);
        sql = objPaginacion.getPaginacion(sql, pagina, 50);
        Sql.getConsulta(sql, dt);
        String[] fila;
        int n = (pagina * 50) - 49;
        while (dt.next()) {
            fila = new String[5];
            fila[0] = Integer.toString(n);
            fila[1] = dt.getString(1);
            fila[2] = dt.getString(2);
            fila[3] = dt.getString(3);
            fila[4] = dt.getString(4);

            modelo.addRow(fila);
            n++;
        }
        objPaginacion.actualPagina(Lpagina);
        objPaginacion.totalPaginas(Lpaginatotal);
        tabla.getSelectionModel().setSelectionInterval(0, 0);
    }

}
